<?php

	include_once('../config.php');
	include_once('class.glossary.php');
	include_once('class.savedSearch.php');
	include_once('misc.php');

	$glossary = new Glossary();
	$savedSearch = new SavedSearch();
	
	$output = array( 'success' => false );
	$outputType = (isset($_REQUEST['outputType'])) ? strtolower($_REQUEST['outputType']) : 'json';

	$mainRefLink = "hide";
	switch($_REQUEST['cmd']) {
		
		case 'listTerms':
			$output = $glossary->getList( $_REQUEST['type'], $_REQUEST['text'],  $_REQUEST['codeFlag']);
			break;

		case 'definition':
			switch( $_REQUEST['type'] ) {
				case 'term':
					$broader = SKOS_PATH . 'terms/letter/' . strtoupper(substr($_REQUEST['value'], 0, 1));
					$hyperlink = ($outputType == "skos") ? false : true;
					$output = array( 'success' => true, 'records' => $glossary->getDefinitionsByTerm($_REQUEST['value'], 'exact', $hyperlink ));
					break;
				case 'letter':
					$broader = SKOS_PATH;
					$mainRefLink = BASE_URL . "terms/letter/" . $_REQUEST['value'];
					$output = array( 'success' => true, 'mainRefLink' => $mainRefLink, 'records' => $glossary->getDefinitionsByLetter($_REQUEST['value']) );
					break;
				case 'code':
					$output = array( 'success' => true, 'records' => $glossary->getDefinitionsByCode($_REQUEST['value']) );
					break;
				case 'code-all':
					$output = array( 'success' => true, 'records' => $glossary->getDefinitionsByCodeAll($_REQUEST['value']) );
					break;
				case 'codeStatus':
					$output = array( 'success' => true, 'records' => $glossary->getDefinitionsByCodeStatus($_REQUEST['value']) );
					break;
				case 'termType':
					$output = array( 'success' => true, 'records' => $glossary->getDefinitionsByTermType($_REQUEST['value']) );
					break;
			}
			$label = $_REQUEST['value'];
			$output['recordCount'] = count($output['records']);
			break;

		case 'search':
			switch( $_REQUEST['searchType'] ) {
				case 'fulltext':
					$highlight = (isset($_REQUEST['highlight'])) ? $_REQUEST['highlight'] : 0;
					if (trim($_REQUEST['query']) == "") {
						$records = array();
					} else {
						$records = $glossary->getDefinitionsByFullText($_REQUEST['query'], $highlight);
						if(!(is_array($records) && count($records))) {
							$records = array();
							$savedSearch->set('query',$_REQUEST['query']);
							$savedSearch->set('searchType',$_REQUEST['searchType']);
							$savedSearch->save();
						# code for new search
							$filter['term'] = $_REQUEST['query'];
							$ret = $savedSearch->listAssociation($filter);
							if(is_array($ret) && count($ret)) {
								foreach($ret as $row) {
									$ar[] = $glossary->getDefinitionsByFullText($row->association, $highlight);
									$records = array_merge($records,$ar);
								}
							}
						}						
					}					
					break;
				default:
					if (trim($_REQUEST['query']) == "") {
						$records = array();
					} else {				
						$records = $glossary->getDefinitionsByTerm($_REQUEST['query'], $_REQUEST['searchType']);
						if(!(is_array($records) && count($records))) {
							$records = array();
							$savedSearch->set('query',$_REQUEST['query']);
							$savedSearch->set('searchType',$_REQUEST['searchType']);
							$savedSearch->save();
						# code for new search
							$filter['term'] = $_REQUEST['query'];
							$ret = $savedSearch->listAssociation($filter);
							if(is_array($ret) && count($ret)) {
								foreach($ret as $row) {
									$ar = $glossary->getDefinitionsByTerm($row->association, $_REQUEST['searchType']);
									$records = array_merge($records,$ar);
								}
							}
						}
					}
					break;
			}
			$output = array( 'success' => true, 'mainRefLink' => BASE_URL . "search/" . urlencode($_REQUEST['query']), 'records' => $records );

			$label = $_REQUEST['query'];
			$output['recordCount'] = count($output['records']);
			break;
		case 'lookup':
			$highlight = (isset($_REQUEST['highlight'])) ? $_REQUEST['highlight'] : 0;
			$records = $glossary->getTerms($_REQUEST['query'], $_REQUEST['searchType']);
			$terms = array();
			if(is_array($records) && count($records)) {
				foreach($records as $record) {
					$terms[] = array('term' => $record->term);
				}
			}
			$output = array( 'success' => true, 'recordCount' => $glossary->db->query_total(), 'records' => $terms );
			break;
		default:
			break;
	}
	
	switch( $outputType ) {
		case 'xml':
		case 'skos':
			require_once('class.skos.php');
			$isTerm = ($_REQUEST['type'] == "term") ? true : false;
			$skos = new SKOS( 1, $output['records'], SKOS_PATH, $label, $_REQUEST['defIndex'], $broader, $isTerm );
			break;
		
		case 'json':
		default:
			header('Content-type: application/json');
			header('charset: utf-8');
			print json_encode( $output );
			break;
	}

?>